@echo off
chcp 65001 >nul 2>&1
echo ==========================================
echo      Gateway Functionality Checker
echo ==========================================
echo.
echo 请确保Gateway服务已手动启动 (端口9000)
echo 如果未启动，请打开新的命令行窗口执行:
echo   cd Demo5_1\Gateway_Server_9000
echo   mvn spring-boot:run
echo.
pause
echo.

echo [1] 基础连接测试...
echo -----------------------------------------
echo 测试Gateway是否启动:
curl -s -w "Status: %%{http_code}\n" http://localhost:9000/test/ping 2>nul
if errorlevel 1 (
    echo ❌ Gateway未启动或连接失败
    echo 请检查Gateway服务是否在端口9000运行
    pause
    exit /b 1
) else (
    echo ✅ Gateway基础连接正常
)
echo.

echo [2] 认证过滤器测试 - 关键诊断...
echo -----------------------------------------
echo 无Token访问(期望: 401 Unauthorized):
curl -s -i http://localhost:9000/test/auth-required 2>nul | findstr /C:"HTTP" /C:"401" /C:"Unauthorized"
if errorlevel 1 (
    echo ❌ 认证过滤器未工作 - 无Token时应返回401
    echo 实际响应:
    curl -s http://localhost:9000/test/auth-required 2>nul
) else (
    echo ✅ 认证过滤器正常工作
)
echo.

echo 有Token访问(期望: 200 OK):
curl -s -w "Status: %%{http_code}\n" -H "Authorization: Bearer mytoken" http://localhost:9000/test/auth-required 2>nul
echo.

echo [3] CORS功能测试...
echo -----------------------------------------
echo OPTIONS预检请求(期望: 200/204):
curl -s -w "Status: %%{http_code}\n" -X OPTIONS -H "Origin: http://localhost:3000" -H "Access-Control-Request-Method: POST" http://localhost:9000/test/ping 2>nul
echo.

echo GET请求CORS头检查:
curl -s -i -H "Origin: http://localhost:3000" http://localhost:9000/test/ping 2>nul | findstr /C:"Access-Control"
if errorlevel 1 (
    echo ❌ 缺少CORS响应头
) else (
    echo ✅ 发现CORS响应头
)
echo.

echo [4] 负载均衡功能测试...
echo -----------------------------------------
echo 测试Provider服务负载均衡:
for /L %%i in (1,1,3) do (
    echo 第%%i次请求:
    curl -s http://localhost:9000/lb-demo/user/instance-info 2>nul | findstr "port"
)
echo.

echo [5] Spring Boot Actuator检查...
echo -----------------------------------------
echo 检查Gateway路由配置:
curl -s http://localhost:9000/actuator/gateway/routes 2>nul | findstr /C:"route_id" /C:"provider"
if errorlevel 1 (
    echo ❌ 无法获取路由信息
) else (
    echo ✅ 路由配置可访问
)
echo.

echo [6] 详细CORS测试...
echo -----------------------------------------
echo 完整CORS预检请求:
curl -v -X OPTIONS -H "Origin: http://localhost:3000" -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: Authorization,Content-Type" http://localhost:9000/test/ping 2>&1 | findstr /C:"HTTP" /C:"Access-Control" /C:"200" /C:"204"
echo.

echo [7] 问题诊断建议...
echo -----------------------------------------
if exist Demo5_1\Gateway_Server_9000\target\classes\ynu\edu\filter\SimpleAuthFilter.class (
    echo ✅ SimpleAuthFilter已编译
) else (
    echo ❌ SimpleAuthFilter未编译 - 需要重新编译项目
)

echo.
echo 如果认证过滤器不工作，可能的原因:
echo 1. @Component注解未被Spring扫描到
echo 2. GlobalFilter未正确注册到Spring Cloud Gateway
echo 3. 过滤器优先级设置有问题
echo 4. Spring Cloud Gateway版本兼容性问题
echo.

echo 如果CORS不工作，可能的原因:
echo 1. application.yml配置语法错误
echo 2. corsConfigurations键名错误
echo 3. 过滤器冲突
echo.

echo ==========================================
echo 检测完成！请查看上面的测试结果
echo ==========================================
pause 